package com.spzx.channel.service.impl;

import com.spzx.channel.service.IIndexService;
import com.spzx.common.core.constant.SecurityConstants;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.product.api.domain.ProductSku;
import com.spzx.product.api.domain.vo.CategoryVo;
import com.spzx.product.api.RemoteCategoryService;
import com.spzx.product.api.RemoteProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author GQS @Date 2024/9/29/10:22 @Description
 */
@Service
public class IndexServiceImpl implements IIndexService {

  @Autowired private RemoteProductService remoteProductService;

  @Autowired private RemoteCategoryService remoteCategoryService;

  @Override
  /**
   * 获取首页展示数据
   *
   * @return 包含分类列表和热销商品列表的Map对象
   * @throws ServiceException 如果远程调用失败，抛出服务异常
   */
  public Map<String, Object> getIndexData() throws ServiceException {
    // 获取分类列表
    R<List<CategoryVo>> categoryListResult =
        remoteCategoryService.getOneCategory(SecurityConstants.INNER);
    // 如果获取分类列表失败，抛出异常
    if (R.FAIL == categoryListResult.getCode()) {
      throw new ServiceException(categoryListResult.getMsg());
    }

    // 获取热销商品列表
    R<List<ProductSku>> productSkuListResult =
        remoteProductService.getTopSale(SecurityConstants.INNER);
    // 如果获取热销商品列表失败，抛出异常
    if (R.FAIL == productSkuListResult.getCode()) {
      throw new ServiceException(productSkuListResult.getMsg());
    }

    // 创建Map对象用于存放首页展示数据
    Map<String, Object> map = new HashMap<>();
    // 将分类列表放入Map对象
    map.put("categoryList", categoryListResult.getData());
    // 将热销商品列表放入Map对象
    map.put("productSkuList", productSkuListResult.getData());
    // 返回包含首页展示数据的Map对象
    return map;
  }
}
